/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.analystest.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A tool class to parse log with given patterns. 
 * 
 * @author Dante Wang
 */
public class ParseUtil {

	public static String parseR(String pattern, String source) {
		Pattern p = Pattern.compile(pattern);

		return parseR(p, source, 0, 1);
	}

	public static String parseR(String pattern, String source, int findIndex) {
		Pattern p = Pattern.compile(pattern);

		return parseR(p, source, findIndex, 1);
	}

	public static String parseR(
			String pattern, ArrayList<String> source, int findIndex) {

		String result = new String();

		for (String s: source) {
			String r = parseR(pattern, s, findIndex);

			if (r==null) {
				continue;
			}

			result += r;
		}

		return result;
	}

	public static String parseR(
			Pattern pattern, String source, int findIndex, int groupIndex) {

		Matcher m = pattern.matcher(source);

		List<String> results = new ArrayList<>();

		while (m.find()) {

			//do something and return the results......
			if (m.groupCount() >= 1) {
				if (groupIndex >=0 && groupIndex <= m.groupCount()) {
					results.add(m.group(groupIndex));
				}
				else {
					throw new IllegalArgumentException(
						"\"groupIndex\" of out bound.");
				}
			}
			else {
				results.add(m.group());
			}
		}

		if (!results.isEmpty()) {
			if (findIndex == -1) {
				return results.get(results.size() - 1);
			}
			else if (findIndex < results.size() && findIndex >= 0) {
				return results.get(findIndex);
			}
			else {
				throw new IllegalArgumentException(
					"\"findIndex\" of out bound.");
			}
		}

		return null;
	}

	public static String parseS(String before, String source) {
		String r = parseS(before, null, source);

		return r;
	}

	public static String parseS(String before, String after, String source) {
		int beforeIndex, afterIndex;

		String r = null;

		if (source == null) {
			return null;
		}

		if (before != null && (beforeIndex = source.indexOf(before)) != -1) {
			if (after != null && (afterIndex = source.indexOf(after)) != -1) {
				r = source.substring(beforeIndex + before.length(), afterIndex);
			}
			else if (after == null)
			{
				r = source.substring(beforeIndex + before.length());
			}
		}

		return r;
	}

	public static String parseS(String before, List<String> source) {
		String r = null;

		for (String line: source) {
			if ((r = parseS(before, line)) != null) {
				return r;
			}
		}

		return r;
	}

	public static String parseS(
			String before, String after, List<String> source) {

		String r = null;

		for (String line: source) {
			if ((r = parseS(before, after, line)) != null) {
				return r;
			}
		}

		return r;
	}
}